package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import test.FlowLayout;
import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.android.phone.mrpc.core.r;
import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.DensityUtil;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.core.utils.XPicasso;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.ActivityToBiaoQian;
import com.itboye.mengwu.app.ui.aizhiwu.GeRenActivity;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.luntan.comment.view.MyListView;

public class YuErJingAdapter extends BaseAdapter {
	String isFav = "0";
	String type;
	OnClickListener imgOnClickListener;
	Context context;
	ArrayList<TieZiBaoHanReply.GetAList> aLists;
	private OnClickListener headclick;
	String uid;
	private String authorID;

	OnClickListener deleteOnclickListener;

	public YuErJingAdapter(Context context, OnClickListener imgOnClickListener,
			OnClickListener headclick,
			ArrayList<TieZiBaoHanReply.GetAList> aLists, String type,
			OnClickListener deleteOnclickListener) {
		this.context = context;
		this.imgOnClickListener = imgOnClickListener;
		this.headclick = headclick;
		this.aLists = aLists;
		this.type = type;
		this.deleteOnclickListener = deleteOnclickListener;
	}

	@Override
	public int getCount() {
		return aLists == null ? 0 : aLists.size();
	}

	@Override
	public Object getItem(int position) {
		return aLists == null ? 0 : aLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings({ "unused", "deprecation" })
	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (holder == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.yuer_list_item, null);
			holder.content_flowlayout = (FlowLayout) convertView
					.findViewById(R.id.content_flowlayout);
			holder.tv = (TextView) convertView.findViewById(R.id.tv);
			holder.question_show = (TextView) convertView
					.findViewById(R.id.question_show);
			holder.author_headewrshow = (ImageView) convertView
					.findViewById(R.id.author_headewrshow);
			holder.baby_sex = (TextView) convertView
					.findViewById(R.id.baby_sex);
			holder.baby_years = (TextView) convertView
					.findViewById(R.id.baby_years);
			holder.pinglunderen = (LinearLayout) convertView
					.findViewById(R.id.pinglunderen);
			holder.biaoqian_lin = (RelativeLayout) convertView
					.findViewById(R.id.biaoqian_lin);
			holder.pinglunrenshu = (TextView) convertView
					.findViewById(R.id.pinglunrenshu);
			holder.pinglun = (MyListView) convertView
					.findViewById(R.id.pinglun);
			holder.author_headewrshow = (ImageView) convertView
					.findViewById(R.id.author_headewrshow);
			holder.collection_num = (TextView) convertView
					.findViewById(R.id.collection_num);
			holder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.gridview);
			holder.author_name = (TextView) convertView
					.findViewById(R.id.author_name);
			holder.delete = (ImageView) convertView.findViewById(R.id.delete);
			holder.imgCollectionNum = (ImageView) convertView
					.findViewById(R.id.imgCollectionNum);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// if (aLists.get(position).getMessage() != null) {
		holder.question_show.setText("[ " + aLists.get(position).getSubject()
				+ " ]" + "\t\t" + aLists.get(position).getMessage());
		// }
		// Log.e("hu", "islike=" + aLists.get(position).getIsLike());
		if ("1".equals(aLists.get(position).getIsLike())) {
			holder.imgCollectionNum.setBackgroundResource(R.drawable.xin_pink);

		} else if ("0".equals(aLists.get(position).getIsLike())) {
			holder.imgCollectionNum
					.setBackgroundResource(R.drawable.like_unselect);
			;
		}

		authorID = aLists.get(position).getAuthorId();
		holder.author_headewrshow.setTag(authorID);
		// Log.d("hu+aID", authorID);
		Log.e("hu+tID", aLists.get(position).getTid());

		holder.author_headewrshow.setOnClickListener(headclick);
		// {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(getActivity(),
		// GeRenActivity.class);
		// intent.putExtra("uid", uid);
		// intent.putExtra("fromUid", authorID);
		// startActivity(intent);
		//
		// }
		// });
		if (aLists.get(position).getTag_info() != null
				&& aLists.get(position).getTag_info().size() != 0) {

			for (int i = 0; i < aLists.get(position).getTag_info().size(); i++) {
				View view = LayoutInflater.from(context).inflate(
						R.layout.textview, null);
				TextView textView = (TextView) view.findViewById(R.id.text);
				textView.setTextColor(context.getResources().getColor(
						R.color.main_color));
				ImageView img_del = (ImageView) view.findViewById(R.id.img_del);
				img_del.setVisibility(View.GONE);
				textView.setBackgroundColor(context.getResources().getColor(
						R.color.white));
				TextView tx[] = new TextView[aLists.get(position).getTag_info()
						.size()];
				textView.setText("#"
						+ aLists.get(position).getTag_info().get(i).getName()
						+ "#");
				android.widget.RelativeLayout.LayoutParams lP = new android.widget.RelativeLayout.LayoutParams(
						android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
						android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
				int right = DensityUtil.dip2px(1);
				lP.setMargins(0, 0, 0, 0);
				view.setTag(i);
				holder.content_flowlayout.addView(view, lP);
				view.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context,
								ActivityToBiaoQian.class);
						intent.putExtra(
								"biaoqian",
								aLists.get(position).getTag_info()
										.get(Integer.parseInt(v.getTag() + ""))
										.getName());

						intent.putExtra(
								"biaoqianID",
								aLists.get(position).getTag_info()
										.get(Integer.parseInt(v.getTag() + ""))
										.getId());
						context.startActivity(intent);
					}
				});
			}
			// holder.tv.setText(builder.toString());
			// builder.delete(0, builder.length());
		} else {
			holder.biaoqian_lin.setVisibility(View.GONE);
		}

		holder.author_name.setText(aLists.get(position).getAuthor());
		if (type.equals("1")) {
			holder.delete.setVisibility(View.VISIBLE);// 育儿经
			holder.delete.setOnClickListener(deleteOnclickListener);// 删除监听
			holder.delete.setTag(aLists.get(position).getTid());
		} else {
			holder.delete.setVisibility(View.GONE);// 帖子
		}
		if (aLists.get(position).getBabyDefault() != null) {
			if ("0".equals(aLists.get(position).getBabyDefault().getSex())) {
				holder.baby_sex.setText("女宝");
			} else if ("1".equals(aLists.get(position).getBabyDefault()
					.getSex())) {
				holder.baby_sex.setText("男宝");

			} else {
				holder.baby_sex.setText("孕育中");
			}
			SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日");
			Calendar calendar = Calendar.getInstance();
			int yearNow = calendar.get(Calendar.YEAR);
			int monthNow = calendar.get(Calendar.MONTH);
			int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
			Calendar calendar2 = Calendar.getInstance();
			if (aLists.get(position).getBabyDefault() != null
					&& aLists.get(position).getBabyDefault().getBirthday() != null) {
				long timews = Long.valueOf(aLists.get(position)
						.getBabyDefault().getBirthday());
				Date date = new Date(timews * 1000L);
				calendar2.setTime(date);
				int yearBirth = calendar2.get(Calendar.YEAR);
				int monthBirth = calendar2.get(Calendar.MONTH);
				int dayBirth = calendar2.get(Calendar.DAY_OF_MONTH);
				int age = yearNow - yearBirth;
				int month = monthNow - monthBirth;
				int day = dayNow - dayBirth;
				if (day < 0) {
					month -= 1;
					calendar2.add(Calendar.MONTH, -1);// 得到上一个月，用来得到上个月的天数。
					day = day
							+ calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
				}
				if (month < 0) {
					month = (month + 12) % 12;
					age--;
				}
				int days = day + 1;
				int months = month + 1;
				if (age == 0) {
					if (month == 0) {
						holder.baby_years.setText(days + "天");
					} else {
						holder.baby_years.setText(months + "月" + days + "天");
					}
				} else {
					if (month == 0) {
						holder.baby_years.setText(age + "岁" + days + "天");
					} else {
						holder.baby_years.setText(age + "岁" + months + "月"
								+ days + "天");
					}
				}
				// if (age > 0) {
				// if (month > 0) {
				// if (day < 0) {
				// month--;
				// age--;
				// }
				// } else if (month < 0) {
				// month += 12;
				// if (day < 0) {
				// month--;
				// age--;
				// }
				// }
				// } else {
				// if (day < 0) {
				// month--;
				// }
				//
				// }
				// SimpleDateFormat myFmt5 = new SimpleDateFormat("d");
				// String birthday2 = myFmt1.format(Long.parseLong((aLists.get(
				// position).getBabyDefault().getBirthday())
				// + "") * 1000L);
				// String birthday = myFmt5.format(System.currentTimeMillis()
				// - Long.parseLong((aLists.get(position).getBabyDefault()
				// .getBirthday()) + "") * 1000L);
				// int days = Integer.parseInt(birthday) % 30;
				// if (age == 0) {
				// if (month == 0) {
				// holder.baby_years.setText(+days + "天");
				// } else if (days == 0) {
				// holder.baby_years.setText(month + "月整");
				// } else if (days != 0) {
				// holder.baby_years.setText(month + "月" + days + "天");
				// }
				// } else if (month == 0) {
				// if (days == 0) {
				// holder.baby_years.setText(age + "岁整");
				// } else {
				// holder.baby_years.setText(age + "岁" + days + "天");
				// }
				// } else if (month != 0) {
				// if (days == 0) {
				// holder.baby_years.setText(age + "岁" + month + "月整");
				// } else {
				// holder.baby_years.setText(age + "岁" + month + "月"
				// + days + "天");
				// }
				// }
			} else {
				holder.baby_sex.setVisibility(View.GONE);
				holder.baby_years.setVisibility(View.GONE);
			}
		} else {
			holder.baby_sex.setVisibility(View.GONE);
			holder.baby_years.setVisibility(View.GONE);
			holder.author_name.setGravity(Gravity.CENTER_VERTICAL);
		}
		if (aLists.get(position).getImg() != null) {
			final ArrayList<String> arrayImage = aLists.get(position).getImg();
			// if (arrayImage == null || arrayImage.size() == 0) { //
			// 没有图片资源就隐藏GridView
			// viewHolder.gridview.setVisibility(View.GONE);
			// } else {
			holder.gridview.setAdapter(new NoScrollGridAdapter(context,
					arrayImage));
			// }
			// 使用ImageLoader加载网络图片
			// XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
			// + aLists.get(position).getHead(), holder.iv_avatar);
			// 点击回帖九宫格，查看大图
			holder.gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					imageBrower(position, arrayImage);
				}
			});

		}

		holder.pinglunderen.removeAllViews();
		if (aLists.get(position).getLikeInfo() != null) {
			for (int i = 0; i < aLists.get(position).getLikeInfo().size(); i++) {
				RoundImageView imageView = new RoundImageView(context);
				LayoutParams layoutParams = new LayoutParams(
						50, LayoutParams.MATCH_PARENT);
				layoutParams.setMargins(5, 0, 5, 0);
				if (aLists.get(position).getLikeInfo().get(i).getHead() == null
						|| "".equals(aLists.get(position).getLikeInfo().get(i)
								.getHead())) {
					if ("1".equals(aLists.get(position).getLikeInfo().get(i)
							.getSex())) {
						// imageView.setImageResource(R.drawable.male);
						imageView.setImageResource(R.drawable.male);
					} else {
						imageView.setImageResource(R.drawable.female);
					}
				} else {
					if (!aLists.get(position).getLikeInfo().get(i).getHead()
							.startsWith("http")) {
						XImageLoader.load(GlobalConfig.API_URL
								+ "/picture/index?id="
								+ aLists.get(position).getLikeInfo().get(i)
										.getHead(), imageView);
					} else {
						XImageLoader.load(aLists.get(position).getLikeInfo()
								.get(i).getHead(), imageView);
					}

				}
				imageView.setTag(R.id.pinglunderen, aLists.get(position)
						.getLikeInfo().get(i).getUid());
				// Log.e("hu  heid", aLists.get(position).getLikeInfo().get(i)
				// .getUid());
				holder.pinglunderen.addView(imageView, layoutParams);
				imageView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, GeRenActivity.class);
						Log.e("hu  tag", v.getTag(R.id.pinglunderen) + "");
						intent.putExtra(
								"uid",
								SPUtils.get(context, null, SpPublicParams.UID,
										"") + "");
						intent.putExtra("fromUid", v.getTag(R.id.pinglunderen)
								+ "");
						context.startActivity(intent);
					}

				});

			}
			holder.pinglunrenshu.setText(aLists.get(position).getReplyInfo()
					.getCount());// 评论人数
		}
		holder.collection_num.setText(aLists.get(position).getLikes());// 收藏人数
		// Log.e("hu====head", aLists.get(0).getHead() + "sex="
		// + aLists.get(0).getSex());
		if (aLists.get(position).getHead() == null
				|| "".equals(aLists.get(position).getHead())) {
			if ("1".equals(aLists.get(position).getSex())) {
				holder.author_headewrshow.setImageResource(R.drawable.male);
			} else {
				holder.author_headewrshow.setImageResource(R.drawable.female);
			}

		} else {
			if (!aLists.get(position).getHead().startsWith("http:")) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getHead(),
						holder.author_headewrshow);
			} else {
				XImageLoader.load(aLists.get(position).getHead(),
						holder.author_headewrshow);
			}

		}
		if (aLists.get(position).getReplyInfo() != null) {
			if (aLists.get(position).getReplyInfo().getCount() != "0") {// 判断是否有回复
				holder.pinglun
						.setAdapter(new YuErJingCommentAdapter(context, aLists
								.get(position).getReplyInfo().getList(),
								position));
				if (aLists.get(position).getReplyInfo().getList().size() > 0)
					System.out.println("评论的条数"
							+ aLists.get(position).getReplyInfo().getList()
									.size());
			}
		}
		holder.imgCollectionNum.setTag(R.id.tag_first, aLists.get(position)
				.getTid());
		holder.imgCollectionNum.setTag(R.id.tag_second, aLists.get(position)
				.getIsLike());
		holder.imgCollectionNum.setOnClickListener(imgOnClickListener);
		holder.imgCollectionNum.setTag(R.id.imgCollectionNum, position);
		return convertView;
	}

	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		context.startActivity(intent);
	}

	class ViewHolder {
		MyListView pinglun;
		TextView author_name, pinglunrenshu, baby_years, baby_sex,
				collection_num;
		NoScrollGridView gridview;
		TextView question_show, hot_talk, tv;
		LinearLayout pinglunderen;
		ImageView author_headewrshow, imgCollectionNum, delete;
		FlowLayout content_flowlayout;
		RelativeLayout biaoqian_lin;
	}
}
